function request(ajaxObj) {
    return new Promise(function (res, rej) {
        var { type = 'get', url, async = 'true', dataType = 'json', data = '' } = ajaxObj
        if (Object.prototype.toString.call(data) === '[object Object]') {
            var array = []
            for (key in data) {
                var val = data[key]
                array.push(key + '=' + val)
            }
            data = array.join('&')
        }
        var xhr = new XMLHttpRequest()
        if (type == 'get') {
            xhr.open('get', data ? url + "?" + data : url, async)
            xhr.send()
        } else if (type == 'post') {
            xhr.open('post', url, async)
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
            xhr.send(data)
        }
        xhr.onreadystatechange = function () {
            try {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var result = xhr.responseText
                    if (dataType == 'json') {
                        result = JSON.parse(result)
                        console.log(result)
                    }
                    res(result)
                }
            } catch (err) {
                rej(err)
            }
        }
    })
}
//---------------------------------------------------------
const isExistUser = params => request({
    type: "get",
    url: "http://43.138.81.225/demo/php/isExistUser.php",
    data: params,
    dataType: "json",
});

const isExistPhone = params => request({
    type: "get",
    url: "http://43.138.81.225/demo/php/isExistPhone.php",
    data: params,
    dataType: "json",
});

const isExistEmail = params => request({
    type: "get",
    url: "http://43.138.81.225/demo/php/isExistEmail.php",
    data: params,
    dataType: "json",
});

const register = params => request({
    type: "post",
    url: "http://43.138.81.225/demo/php/register.php",
    data: params,
    dataType: "json",
});

const loginAccount = params => request({
    type: "post",
    url: "http://43.138.81.225/demo/php/login_account.php",
    data: params,
    dataType: "json",
});

const searchAllGoods = params => request({
    type: "get",
    url: "http://43.138.81.225/demo/php/searchAllGoods.php",
    data: params,
    dataType: "json",
});

const searchGoodsOrderLimit = params => request({
    type: "get",
    url: "http://43.138.81.225/demo/php/searchGoodsOrderLimit.php",
    data: params,
    dataType: "json",
});


const searchGoodsByGoodsId = params => request({
    type: "get",
    url: "http://43.138.81.225/demo/php/searchGoodsByGoodsId.php",
    data: params,
    dataType: "json",
});

const addToShoppingCar = params => request({
    type: "post",
    url: "http://43.138.81.225/demo/php/addToShoppingCar.php",
    data: params,
    dataType: "json",
});

const searchShoppingCarByUser = params => request({
    type: "post",
    url: "http://43.138.81.225/demo/php/searchShoppingCarByUser.php",
    data: params,
    dataType: "json",
});

const updateShoppingCarById = params => request({
    type: "post",
    url: "http://43.138.81.225/demo/php/updateShoppingCarById.php",
    data: params,
    dataType: "json",
});

const deleteShoppingCarById = params => request({
    type: "get",
    url: "http://43.138.81.225/demo/php/deleteShoppingCarById.php",
    data: params,
    dataType: "json",
});






//cookie
function setCookie(key, val, expires, path = "/") {
    if (expires) {
        var date = new Date()
        date.setSeconds(date.getSeconds() + expires);
        document.cookie = key + "=" + val + "; expires=" + date.toUTCString() + "; path=" + path
    } else {
        document.cookie = key + "=" + val + "; path=" + path
    }
}

function getCookie(key) {
    var cookie = document.cookie;
    if (cookie) {
        var list = cookie.split("; ")
        for (var i = 0; i < list.length; i++) {
            var item = list[i]
            var name = item.split("=")[0]
            var val = item.split("=")[1]
            if (name === key) {
                return val;
            }
        }
    }
    return "";
}

function delCookie(key) {
    setCookie(key, "", -1);
}




function urlParamsParse() {

    var search = location.search;  // ?keyword=%E5%8D%8E%E4%B8%BA&enc=utf-8&wq=%E5%8D%8E%E4%B8%BA&pvid=2594ef2dec5d4ac396b50ef99d4569a5
    var data = {}
    if (search) {
        // 将数据拆分为指定条数  ("&")
        var str = search.substring(1); // 从下标为1 截取到最后
        var list = str.split("&");
        // console.log(list); // ['user=a123123', 'pwd=123123', 'phone=17386151421', 'email=123132@qq.com']
        // 取出每一条的键名和键值  ("=")
        list.forEach(item => {  // 'user=a123123'
            var key = item.split("=")[0];
            var val = item.split("=")[1];
            data[key] = decodeURIComponent(val); // 数据解析成功之后 在对键值解码
        })
    }
    return data;
}






function ajax(ajaxObject) {
    var { type = 'get', url, data = '', async = true, dataType = 'json', callback, error } = ajaxObject
    if (typeof data == 'object') { // 判断data是对象(不准确)
        var list = []
        for (var key in data) {
            var val = data[key]
            list.push(key + '=' + val)
        }
        data = list.join('&')
    }
    var xhr = new XMLHttpRequest()
    if (type.toLowerCase() == 'get') {
        xhr.open('get', data ? url + '?' + data : url, async)
        xhr.send()
    } else if (type.toLowerCase() == 'post') {
        xhr.open('post', url, async)
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        xhr.send(data)
    }
    xhr.onreadystatechange = function () {
        try {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var result = xhr.responseText
                if (dataType == 'json') {
                    result = JSON.parse(result)
                }
                if (callback && typeof callback === 'function') {
                    callback(result)
                }
            }
        } catch (err) {
            if (error && typeof error == 'function') {
                error(err)
            }
        }
    }
}
